package chatbot.base.event;

import java.util.SortedSet;
import java.util.Comparator;
import java.util.Iterator;
import java.util.concurrent.ConcurrentSkipListSet;


public final class SimpleEventManager implements EventManager {
    private static final Comparator<Event> compare = new Comparator<Event>() {
        public int compare(Event i, Event j) {
            int result = i.getName().compareTo(j.getName());
            return result;
        }
    };
    private SortedSet events = new ConcurrentSkipListSet(compare);


    public void registerListener(Listener l, String e) throws Exception {
        Iterator<Event> i = events.iterator();
        while (i.hasNext()) {
            Event temp = i.next();
            if (temp.getName().compareTo(e) == 0) {
                temp.registerListener(l);
                return;
            }
        }
        throw new Exception("Event '" + e + "' is not registered.");
    }
    public void registerEvent(Event e) throws Exception {
        if (events.contains(e)) throw new Exception("Event '" + e + "' is already registered.");
        events.add(e);
    }
}
